SingleTon and Adapter Examples
class adder_i{ int a,b; public adder_i(){} public adder_i(int x,int y){ a = x ; b = y; } public void add(){ System.out.println("Sum of "+a+" km and "+b+" km = "+(a+b)+" km."); } } class adder_d{ double a,b; public adder_d(){} public adder_d(double x,double y){ a = x ; b = y; } public double get_a(){return a;} public double get_b(){return b;} public void add(){ System.out.println("Sum of "+a+" m and "+b+" m = "+(a+b)+" m."); } } class adder_adapter{ public void convert_d_to_i(adder_d d){ int a = (int)d.get_a(); int b = (int)d.get_b(); adder_i i = new adder_i(a,b); i.add(); } } class adapter_demo{ public static void main(String[] argggs){ adder_i a = new adder_i(2,3); a.add(); adder_d d = new adder_d(3.0,4.0); d.add(); adder_adapter aa = new adder_adapter(); aa.convert_d_to_i(d); } }
class Single_instance{ public int value; private static Single_instance si = new Single_instance(); private Single_instance(){} public void setValue(int v){ value = v; } public static Single_instance getInstance(){ return si; } } class Single_instance_demo{ public static void main(String[] aargs){ Single_instance s = Single_instance.getInstance(); s.setValue(44); Single_instance s1 = Single_instance.getInstance(); System.out.println(s.value); System.out.println(s1.value); s1.setValue(99); System.out.println(s.value); System.out.println(s1.value); } }